/*
 * can_task.c
 *
 *  Created on: 2023��4��11��
 *      Author: chenwangang
 */



/* SDK includes. */
#include "interrupt_manager.h"
#include <string.h>
#include "user_define.h"
#include "bsp_can_cfg.h"
#include "bsp_can_driver.h"
#include "pwm_film_light.h"

#define CAN_RCV_WAITTIME    ( 5UL / portTICK_PERIOD_MS )    

#if(CAN_USED == TRUE)

uint8_t gate_flag_can = 0, htcu_flag_can = 0, sensor_flag_can, musicloud_flag_can = 0;

extern void message_processing(void);

extern union protocol_64bit_GATE g_gate_64bits;
extern union protocol_64bit_HTCU g_htcu_64bits;
extern union protocol_64bit_SENSOR g_sensor_64bits;
extern union protocol_64bit_MUSICLOUD g_musicloud_64bits;
extern union protocol_SendMsg g_send_msg;

uint8_t gate_flag_film = 0;
uint8_t htcu_flag_film = 0;
uint8_t sensor_flag_film = 0;
uint8_t musicloud_flag_film = 0;

union protocol_64bit_GATE g_temp_gate_data_film;
union protocol_64bit_HTCU g_temp_htcu_data_film;
union protocol_64bit_SENSOR g_temp_sensor_data_film;
union protocol_64bit_MUSICLOUD g_temp_musicloud_data_film;

extern QueueHandle_t g_xQueue_Rx_Can_Msg1;
extern QueueHandle_t g_xQueue_Rx_Can_Msg2;
extern QueueHandle_t g_xQueue_Rx_Can_Msg3;
extern QueueHandle_t g_xQueue_Rx_Can_Msg4;

/*-----------------------------------------------------------*/
void recv_canMsg(void)
{
	BaseType_t ret_recv = pdFALSE;
	static uint8_t prev_gate_buf[8], prev_htcu_buf[8];

	/****Receives the data from the queue into canRecvMsg****/
	xQueueReceive( g_xQueue_Rx_Can_Msg1, &canRecvMsg, CAN_RCV_WAITTIME);
    xQueueReceive( g_xQueue_Rx_Can_Msg2, &canRecvMsg2, CAN_RCV_WAITTIME);
    xQueueReceive( g_xQueue_Rx_Can_Msg3, &canRecvMsg3, CAN_RCV_WAITTIME);
    xQueueReceive( g_xQueue_Rx_Can_Msg4, &canRecvMsg4, CAN_RCV_WAITTIME);
	

    if (canRecvMsg.id == GATE_SUB_1)
    {
        memcpy(g_gate_64bits.data_buffer, canRecvMsg.data, 8);
        memcpy(g_temp_gate_data_film.data_buffer, canRecvMsg.data, 8);
        gate_flag_can = 1;
        gate_flag_film = 1;
    }

    if(canRecvMsg2.id == HTCU_VCU_1)
    {
        memcpy(g_htcu_64bits.data_buffer, canRecvMsg.data, 8);
        memcpy(g_temp_htcu_data_film.data_buffer, canRecvMsg.data, 8);
        htcu_flag_can = 1;
        htcu_flag_film = 1;
    }

    if(canRecvMsg3.id == SENSOR_1)
    {
        memcpy(g_sensor_64bits.data_buffer, canRecvMsg.data, 8);
        memcpy(g_temp_sensor_data_film.data_buffer, canRecvMsg.data, 8);
        sensor_flag_can = 1;
        sensor_flag_film = 1;
    }

    if(canRecvMsg4.id == MUSICLOUD_1)
    {
        memcpy(g_musicloud_64bits.data_buffer, canRecvMsg.data, 8);
        memcpy(g_temp_musicloud_data_film.data_buffer, canRecvMsg.data, 8);
        musicloud_flag_can = 1;
        musicloud_flag_film = 1;
    }
}

void message_delete_can(void)
{
	for (int j=0; j<8; j++)
	{
		g_htcu_64bits.data_buffer[j] = 0;
		g_gate_64bits.data_buffer[j] = 0;
		g_musicloud_64bits.data_buffer[j] = 0;
	}
}

void message_processing(void)
{
    if (gate_flag_can != 0 && htcu_flag_can != 0 && musicloud_flag_can != 0)
    {
        /***flag clear***/
        gate_flag_can  = 0;
		htcu_flag_can = 0;
        musicloud_flag_can = 0;

        /*************STOP**************/
        if (g_htcu_64bits.ptrHTCU.ESP_APA_Target_Gear_Request == P_GEAR)	//P Gear
        {
            //Search car
            if (g_gate_64bits.ptrGATE.TBOX_SearchVehicleReq == ACTIVE && g_gate_64bits.ptrGATE.OrderInformation != DISTANCE_CLOSE)
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_SEARCH_CAR;
            }

            //Welcome
            if (g_gate_64bits.ptrGATE.OrderInformation == DISTANCE_CLOSE && g_gate_64bits.ptrGATE.SmartSystemWarning3_1 != BATTERY_LOW)
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_WELCOME;
            }

            //Open Door
            if (g_gate_64bits.ptrGATE.DriverDoorSts == ACTIVE || g_gate_64bits.ptrGATE.LHRDoorSts == ACTIVE
                || g_gate_64bits.ptrGATE.PsngrDoorSts == ACTIVE || g_gate_64bits.ptrGATE.RHRDoorSts == ACTIVE)
            {

                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_DOOR_OPEN;
            }
            
            //Farewell
            if (g_gate_64bits.ptrGATE.DriverDoorSts == CLOSE && g_gate_64bits.ptrGATE.OrderInformation == LOCK)
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_FAREWELL;
                
            }

            //Trions
            if (g_musicloud_64bits.ptrMUSIC.LiShowMod == TRIONES)  
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_TRIONES;
            }
            
            //Holiday
            if (g_musicloud_64bits.ptrMUSIC.LiShowMod == HOLIDAY)  
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_HOLIDAY;
            }

            //Self-define img
            if (g_musicloud_64bits.ptrMUSIC.LiShowMod == DIY_IMG) 
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_DIY_IMG;
            }

            //Child
            if (g_gate_64bits.ptrGATE.OrderInformation == LOCK)
            {
                g_send_msg.ptrDOMAIN_BEHIND.CAR_Sts = 0x2;
            }
            else
            {
                g_send_msg.ptrDOMAIN_BEHIND.CAR_Sts = 0x1;
            }
        }

        /**************Driving*************/
        if (g_htcu_64bits.ptrHTCU.ESP_APA_Target_Gear_Request != CLOSE && g_htcu_64bits.ptrHTCU.EngineSpeed != 0)
        {
            //Comity
            if (g_gate_64bits.ptrGATE.BCM_20_BrkLampSts == ACTIVE && g_htcu_64bits.ptrHTCU.VehicleSpeedVSOSig == 0)
            {
                if (g_musicloud_64bits.ptrMUSIC.MusicShowCMD == ACTIVE)
                {
                    g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_COMITY;
                }
            }

            //Temp Stop
            if (g_htcu_64bits.ptrHTCU.VehicleSpeedVSOSig == 0)
            {
                if (g_gate_64bits.ptrGATE.RHTurnlightSts == ACTIVE && g_gate_64bits.ptrGATE.LHTurnlightSts == ACTIVE)
                {
                    g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_TEMP_STOP;
                }
            }

            //Turn
            if (g_htcu_64bits.ptrHTCU.VehicleSpeedVSOSig != 0 && (g_gate_64bits.ptrGATE.SteeringAngle != ANGLE720 && g_gate_64bits.ptrGATE.SteeringAngle != ANGLE_720))
            {
                if (g_gate_64bits.ptrGATE.LHTurnlightSts == ACTIVE)   //左转
                {
                    g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_TURN_LEFT;
                }
                if (g_gate_64bits.ptrGATE.RHTurnlightSts == ACTIVE)   //右转
                {
                    g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_TURN_RIGHT;
                }
            }

            //U-Turn
            if ((g_gate_64bits.ptrGATE.LHTurnlightSts == ACTIVE || g_gate_64bits.ptrGATE.RHTurnlightSts == ACTIVE) &&
                    (g_gate_64bits.ptrGATE.SteeringAngle == ANGLE720 || g_gate_64bits.ptrGATE.SteeringAngle == ANGLE_720))
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_U_TURN;
            }
            
            //R Gear
            if (g_htcu_64bits.ptrHTCU.ESP_APA_Target_Gear_Request == R_GEAR)
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_BACK;
            }

            //Brake
            if (g_gate_64bits.ptrGATE.BLRequestController == ACTIVE)
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_EMERGENCY_BRAKE;
            }

            //Auto
            if (g_htcu_64bits.ptrHTCU.APA_Active == ACTIVE || g_htcu_64bits.ptrHTCU.MEB_LedReq == ACTIVE)
            {
                g_send_msg.ptrDOMAIN_BEHIND.TAIL_Sts = TAIL_AUTO_DRIVE;
            }

        }

        if (g_gate_64bits.ptrGATE.DriverDoorSts == ACTIVE)
        {
            g_send_msg.ptrDOMAIN_BEHIND.DriverDoorSts = ACTIVE;
        }
        else
        {
            g_send_msg.ptrDOMAIN_BEHIND.DriverDoorSts = CLOSE;
        }

        if (g_gate_64bits.ptrGATE.PsngrDoorSts == ACTIVE)
        {
            g_send_msg.ptrDOMAIN_BEHIND.PsngrDoorSts = ACTIVE;
        }
        else
        {
            g_send_msg.ptrDOMAIN_BEHIND.PsngrDoorSts = CLOSE;
        }

        if (g_gate_64bits.ptrGATE.RHRDoorSts == ACTIVE)
        {
            g_send_msg.ptrDOMAIN_BEHIND.RHRDoorSts = ACTIVE;
        }
        else
        {
            g_send_msg.ptrDOMAIN_BEHIND.RHRDoorSts = CLOSE;
        }

        if (g_gate_64bits.ptrGATE.LHRDoorSts == ACTIVE)
        {
            g_send_msg.ptrDOMAIN_BEHIND.LHRDoorSts = ACTIVE;
        }
        else
        {
            g_send_msg.ptrDOMAIN_BEHIND.LHRDoorSts = CLOSE;
        }

        //Signal clear
	    message_delete_can();
    }


    
}

void Can_Send(void)
{
    uint8_t databuff[8] = {1,2,3,4,5,6,7,8};
    // memcpy(canSendMsg.data, test_data_arr, 8);
    memcpy(canSendMsg.data, databuff, 8);
    CAN_Send(&can_pal1_instance, g_tx_msg_config[1].ucTxMailBox, &canSendMsg);
}

can_message_t SendMsg = {
    .cs     = 0,
    .length = 8,
    .data = {1,2,3,4,5,6,7,8},
};;
void General_Can_Send(uint8_t index)
{
    uint8_t databuff[8] = {1,2,3,4,5,6,7,8};
    

    

#if 1
    switch (index)
    {
    case 1:
        SendMsg.id = TX_ID_1;
        // memcpy(SendMsg.data, databuff, 8);
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[2].ucTxMailBox, &SendMsg, 2);
        break;
    case 2:
        SendMsg.id = TX_ID_2;
        // memcpy(SendMsg.data, databuff, 8);
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[3].ucTxMailBox, &SendMsg, 2);
        break;
    case 3:
        SendMsg.id = TX_ID_3;
        // memcpy(SendMsg.data, databuff, 8);
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[4].ucTxMailBox, &SendMsg, 2);
        break;
    case 4:
        SendMsg.id = TX_ID_4;
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[5].ucTxMailBox, &SendMsg, 2);
        break;
    case 5:
        SendMsg.id = TX_ID_5;
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[6].ucTxMailBox, &SendMsg, 2);
        break;
    case 6:
        SendMsg.id = TX_ID_6;
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[7].ucTxMailBox, &SendMsg, 2);
        break;
    case 7:
        SendMsg.id = TX_ID_7;
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[8].ucTxMailBox, &SendMsg, 2);
        break;
    case 8:
        SendMsg.id = TX_ID_8;
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[9].ucTxMailBox, &SendMsg, 2);
        break;
    case 9:
        SendMsg.id = TX_ID_9;
        CAN_SendBlocking(&can_pal1_instance, g_tx_msg_config[10].ucTxMailBox, &SendMsg, 2);
        break;

    default:
        break;
    }
#endif
}

void can_task(void)
{
    recv_canMsg();
    message_processing();
}

#endif


